package com.app.service.impl;

import com.app.mapper.PositionMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @Description:行业
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
@Log4j2
public class PositionServiceImpl implements PositionService {
    @Autowired
    PositionMapper positionMapper;
    @Autowired
    CustomerService customerService;
    @Autowired
    ComRecruitersService comRecruitersService;
    @Autowired
    JobPublishService jobPublishService;

    @Autowired
    private JobIntentionService jobIntentionService;

    @Override
    public PositionModel getModelById(int id) {

        return positionMapper.selectById(id);
    }

    @Override
    public PositionModel getModelByCode(String postCode) {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("post_code", postCode);
        List<PositionModel> userList = positionMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public List<PositionModel> findName(int level, int postType) {

        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("level", level);
        queryWrapper.select().eq("post_type", postType);
        queryWrapper.orderByDesc("crt_time");
        List<PositionModel> userList = positionMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public List<PositionModel> getModelListByLevel(int level) {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("level", level);
        List<PositionModel> userList = positionMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public List<PositionModel> getModelListByFather(UserModel user) {
        Integer type = null;
        CustomerModel cus = new CustomerModel();
        if (user != null) {
            cus = customerService.getCustomerById(user.getCId());
        }
        if (cus != null) {
            type = cus.getId();
        }
        List<JobIntentionModel> jobIntentionModelList = jobIntentionService.getIntentionListByCustomerId(type);
        List<PositionModel> positionModelList = new ArrayList<>();
        if (jobIntentionModelList == null) {
            positionModelList.addAll(getModelListByLevel(3));
        } else {
            for (int i = 0; i < jobIntentionModelList.size(); i++) {
                PositionModel positionModel = getModelById(jobIntentionModelList.get(i).getPost());
                positionModelList.addAll(getModelListByFatherId(Integer.valueOf(positionModel.getFatherCode())));
            }
        }

        List<PositionModel> newPositionModelList = new ArrayList<>();
        Random ra = new Random();
        for (int i = 0; i < positionModelList.size(); i++) {
            int reKey = ra.nextInt(positionModelList.size());
            newPositionModelList.add(positionModelList.get(reKey));
            HashSet h = new HashSet(newPositionModelList);
            newPositionModelList.clear();
            newPositionModelList.addAll(h);
            if (newPositionModelList.size() >= 8) {
                break;
            }

        }
        return newPositionModelList;
    }

    @Override
    public List<PositionModel> getModelListByFatherCom(UserModel user) {
        ComRecruitersModel com = comRecruitersService.getComRecruitersById(user.getRId());
        List<JobPublishModel> jobPublishModelList = jobPublishService.getJobPublishListByExOpen(com.getId());
        List<PositionModel> positionModelList = new ArrayList<>();
        if (jobPublishModelList == null) {
            positionModelList.addAll(getModelListByLevel(3));
        } else {
            for (int i = 0; i < jobPublishModelList.size(); i++) {
                PositionModel positionModel = getModelById(jobPublishModelList.get(i).getJobType());
                positionModelList.addAll(getModelListByFatherId(Integer.valueOf(positionModel.getFatherCode())));
            }
        }
        List<PositionModel> newPositionModelList = new ArrayList<>();
        Random ra = new Random();
        for (int i = 0; i < positionModelList.size(); i++) {
            int reKey = ra.nextInt(positionModelList.size());
            newPositionModelList.add(positionModelList.get(reKey));
            HashSet h = new HashSet(newPositionModelList);
            newPositionModelList.clear();
            newPositionModelList.addAll(h);
            if (newPositionModelList.size() >= 8) {
                break;
            }
        }
        return newPositionModelList;
    }


    @Override
    public List<PositionModel> findNameJid(String fatherCode, int postType) {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("father_code", fatherCode);
        queryWrapper.select().eq("post_type", postType);
        List<PositionModel> userList = positionMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public List<PositionModel> getModelListByFatherId(int fatherId) {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "job_title","father_code","post_code").eq("father_code", fatherId);
        List<PositionModel> userList = positionMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }


    @Override
    public int insert(PositionModel model) {
        return positionMapper.insert(model);
    }

    @Override
    public void update(PositionModel model) {
        positionMapper.updateById(model);
    }

    @Override
    public void deleteByFatherCode(String fatherCode) {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("father_code", fatherCode);
        positionMapper.delete(queryWrapper);
    }

    @Override
    public void delete(PositionModel model) {
        positionMapper.deleteById(model.getId());
    }

    @Override
    public List<PositionModel> parent() {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", 1);
        return positionMapper.selectList(queryWrapper);
    }

    @Override
    public List<PositionModel> childLevel() {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", 2);
        return positionMapper.selectList(queryWrapper);
    }

    @Override
    public List<PositionModel> grandchildLevel() {
        QueryWrapper<PositionModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", 3);
        return positionMapper.selectList(queryWrapper);
    }
}
